package com.jeesite.modules.rt.api;

import com.alipay.v3.ApiException;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.config.MoneyParamter;
import com.jeesite.modules.config.Result;
import com.jeesite.modules.ic.entity.IcUser;
import com.jeesite.modules.ic.service.IcUserService;
import com.jeesite.modules.mo.entity.MoShouyi;
import com.jeesite.modules.mo.entity.MoTixian;
import com.jeesite.modules.mo.service.MoShouyiService;
import com.jeesite.modules.mo.service.MoTixianService;
import com.jeesite.modules.mo.service.MoneyService;
import com.jeesite.modules.utils.ali.AlipayFundTransUniApiTransfer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@RestController
//@RequestMapping("/api/money")
@RequestMapping("/app/api/money")
public class ApiMoneyController extends BaseController {

    @Autowired
    private MoShouyiService moShouyiService;
    @Autowired
    private MoTixianService moTixianService;
    @Autowired
    private IcUserService icUserService;
    @Autowired
    private MoneyService moneyService;


    /**
     * 提现详情（主要检测是否提现成功）
     * @param moTixian
     * @param request
     * @return
     */
    @RequestMapping("tixianInfo")
    public Result tixianInfo(MoTixian moTixian,HttpServletRequest request){
        moTixian = moTixianService.get(moTixian);
        return Result.buildSuccessResult(moTixian);
    }

    /**
     *
     * @return
     */
    @RequestMapping("tixianCommit")
    public Result tixianCommit(MoTixian moTixian,HttpServletRequest request){
        String userId = getUserId(request);
        moTixian.setUserId(userId);
        IcUser icUser = icUserService.get(userId);
        if(StringUtils.isEmpty(icUser.getZhifubaoMobile()) || StringUtils.isEmpty(icUser.getZhifubaoMobile())){
            return Result.buildFailureResult("请完善支付宝收款信息",5001);
        }
        if(icUser.getYue()<moTixian.getMoney()){
            return Result.buildFailureResult("余额不足");
        }
        if(moTixian.getType()== MoneyParamter.TIXIAN_TYPE_ALI){//支付宝

                try {
                    if(moneyService.aliTixian(moTixian)){//成功
                        return Result.buildSuccessResult(moTixian);
                    }else{
                        return Result.buildFailureResult("提现失败");
                    }
                } catch (ApiException e) {
                    return Result.buildFailureResult("提现失败");
                }

        }
        return Result.buildSuccessResult();
    }

    /**
     * 提成收益明细
     */
    @RequestMapping("getTichengListByMonth")
    public Result getTichengListByMonth(HttpServletRequest request, HttpServletResponse response){
        String user_id = getUserId(request);
        String month = request.getParameter("month");
        MoShouyi moShouyi = new MoShouyi();
        if(StringUtils.isNotEmpty(month)){
            long i = Long.valueOf(month).longValue();
            Date date = new Date(i);
            int monthHasDays = DateUtils.getMonthHasDays(date);
            String monthStr = DateUtils.formatDate(date, "yyyy-MM");
            String startDate = monthStr+"-01 00:00:00";
            String endDate = monthStr+"-"+monthHasDays+" 23:59:59";
            moShouyi.setWorkDateStart(startDate);
            moShouyi.setWorkDateEnd(endDate);
        }
        moShouyi.setUserId(user_id);
        moShouyi.setType(2);
        moShouyi.setPage(new Page<>(request,response));
        Page<MoShouyi> page = moShouyiService.findPage(moShouyi);
        List<MoShouyi> list = page.getList();

        for (MoShouyi moShouyi1 : list) {
            String nextUserId = moShouyi1.getNextUserId();
            IcUser nextUser = icUserService.get(nextUserId);
            if (nextUser != null) {
                if(StringUtils.isEmpty(nextUser.getAvatarUrl())){
                    nextUser.setAvatarUrl("../static/touxiang.png");
                }
                if(StringUtils.isEmpty(nextUser.getNicheng())){
                    nextUser.setNicheng("未知");
                }
                moShouyi1.setNextUser(nextUser);
            }
        }
        
        return Result.buildSuccessResult(list);
    }

    /**
     * 提现明细
     * @return
     */
    @RequestMapping("getTixianMingxi")
    public Result getTixianMingxi(HttpServletRequest request, HttpServletResponse response){
        String user_id = getUserId(request);
        MoTixian moShouyi = new MoTixian();
        IcUser icUser = icUserService.get(user_id);
        moShouyi.setUserId(user_id);
        moShouyi.setPage(new Page<>(request,response));
        Page<MoTixian> page = moTixianService.findPage(moShouyi);
        for (MoTixian moShouyi1 : page.getList()) {
            if(StringUtils.isEmpty(icUser.getAvatarUrl())){
                icUser.setAvatarUrl("../static/touxiang.png");
            }
            if(StringUtils.isEmpty(icUser.getNicheng())){
                icUser.setNicheng("未知");
            }
            moShouyi1.setIcUser(icUser);
        }
        return Result.buildSuccessResult(page);
    }

    /**
     * 收益明细
     * @return
     */
    @RequestMapping("getShouyiMingxi")
    public Result getShouyiMingxi(HttpServletRequest request, HttpServletResponse response){
        String user_id = getUserId(request);
        String typeSearch = request.getParameter("type");
        String month = request.getParameter("month");
        MoShouyi moShouyi = new MoShouyi();
        if(StringUtils.isNotEmpty(month)){
            long i = Long.valueOf(month).longValue();
            Date date = new Date(i);
            int monthHasDays = DateUtils.getMonthHasDays(date);
            String monthStr = DateUtils.formatDate(date, "yyyy-MM");
            String startDate = monthStr+"-01 00:00:00";
            String endDate = monthStr+"-"+monthHasDays+" 23:59:59";
            moShouyi.setWorkDateStart(startDate);
            moShouyi.setWorkDateEnd(endDate);
        }
        if(StringUtils.isNotEmpty(typeSearch)){
            moShouyi.setType(Integer.parseInt(typeSearch));
        }
        moShouyi.setUserId(user_id);
        moShouyi.setPage(new Page<>(request,response));
        Page<MoShouyi> page = moShouyiService.findPage(moShouyi);
        List<MoShouyi> list = page.getList();
        for (MoShouyi moShouyi1 : list) {
            Integer type = moShouyi1.getType();
            if(type == 2){
                String nextUserId = moShouyi1.getNextUserId();
                IcUser icUser = icUserService.get(nextUserId);
                if(StringUtils.isEmpty(icUser.getAvatarUrl())){
                    icUser.setAvatarUrl("../static/touxiang.png");
                }
                if(StringUtils.isEmpty(icUser.getNicheng())){
                    icUser.setNicheng("未知");
                }
                moShouyi1.setNextUser(icUser);
            }
        }
        return Result.buildSuccessResult(page.getList());
    }

    public String getUserId(HttpServletRequest request){
        Object user_id = request.getAttribute("user_id");
        if(user_id!=null){
            return user_id.toString();
        }
        return request.getParameter("userId");
    }
}
